package com.qdlc.p2p.biz.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.qdlc.p2p.biz.service.UpRateService;
import com.qdlc.p2p.common.util.DateUtil;
import com.qdlc.p2p.common.util.StringUtil;
import com.qdlc.p2p.dal.common.model.jpa.OrderFilter.OrderType;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.common.model.jpa.QueryParam;
import com.qdlc.p2p.dal.common.model.jpa.SearchFilter.Operators;
import com.qdlc.p2p.dal.dao.RateInterestDao;
import com.qdlc.p2p.dal.dao.RateRuleDao;
import com.qdlc.p2p.dal.dao.RateRuleDetailDao;
import com.qdlc.p2p.dal.dao.UpRateCollectionDao;
import com.qdlc.p2p.dal.dto.RateInterest;
import com.qdlc.p2p.dal.dto.RateRule;
import com.qdlc.p2p.dal.dto.RateRuleDetail;
import com.qdlc.p2p.dal.model.RateInterestModel;
import com.qdlc.p2p.dal.model.RateRuleModel;
import com.qdlc.p2p.dal.mybatis.model.mapper.RateInterestMapper;
import com.qdlc.p2p.dal.mybatis.pager.PageProxyBuilder;
import com.qdlc.p2p.dal.mybatis.pager.core.PageParameter;

@Repository("upRateService")
@Transactional
public class UpRateServiceImpl implements UpRateService {

	@Resource
	private RateRuleDao rateRuleDao;
	@Resource
	private RateRuleDetailDao rateRuleDetailDao;
	@Resource
	private RateInterestDao rateInterestDao;
	@Resource
	private UpRateCollectionDao upRateCollectionDao;
	
	@Override
	public RateRule getRateRuleById(long id) {
		return rateRuleDao.find(id);
	}

	@Override
	public void addUprateRule(RateRule rate,List<RateRuleDetail> detailList) {
		rateRuleDao.save(rate);
		//如果存在规则详情，就保存
		if (null !=detailList && detailList.size()>0) {
			for (RateRuleDetail detail : detailList) {
				detail.setPayWay(rate.getPayWay());
				detail.setAddTime(DateUtil.getNow());
				detail.setRateRuleId(rate.getId());
				detail.setIsDelete(1);
			}
			rateRuleDetailDao.save(detailList);
		}
	}

	@Override
	public PageDataList<RateRule> getRateRulePage(RateRuleModel rateRuleModel) {
		QueryParam param = QueryParam.getInstance();
		
		if (StringUtil.isNotBlank(rateRuleModel.getIsUse()+"") && "99".equals(rateRuleModel.getIsUse())) {
			param.addParam("isUse", rateRuleModel.getIsUse());
		}
		
		if (!StringUtil.isBlank(rateRuleModel.getSearchName())) {
			param.addParam("ruleName",Operators.LIKE, rateRuleModel.getSearchName());
		}
		if (!StringUtil.isBlank(rateRuleModel.getExpiredStartTime())) {  //判断时间
			param.addParam("endTime",Operators.GTE, DateUtil.valueOf(rateRuleModel.getExpiredStartTime()));
		}
		
		if (!StringUtil.isBlank(rateRuleModel.getExpiredStartTime())) {  //判断时间
			param.addParam("endTime",Operators.LTE, DateUtil.valueOf(rateRuleModel.getExpiredEndTime()));
		}
		param.addPage(rateRuleModel.getPage(), rateRuleModel.getRows());
		param.addOrder(OrderType.DESC, "id");
		PageDataList<RateRule> list = rateRuleDao.findPageList(param);
		return list;
	}

	@Override
	public void updateRateRule(RateRule rule,List<RateRuleDetail> list) {
		rateRuleDao.update(rule);
		//跟新规则时，判断是否有子规则，如果有子规则，全部删除
		rateRuleDetailDao.deleteByRuleId(rule.getId());
		//重新插入新的子规则
		if (null !=list && list.size()>0) {
			for (RateRuleDetail detail : list) {
				detail.setPayWay(rule.getPayWay());
				detail.setAddTime(DateUtil.getNow());
				detail.setRateRuleId(rule.getId());
				detail.setIsDelete(1);
			}
			rateRuleDetailDao.save(list);
		}
	}

	@Override
	public int updateStatus(long id, int nowStatus, int oldStatus) {
		return rateRuleDao.updateRuleStatus(id, oldStatus, nowStatus);
		
	}

	@Override
	public List<RateRuleDetail> getDetailByRuleId(long ruleId) {
		return	rateRuleDetailDao.getrateDetailListByRid(ruleId);
	}

	@Override
	public PageDataList<RateInterestModel> getRateInteModelList(RateInterestModel model) {
		return rateInterestDao.getAllRateInterest(model.getSearchName(), model.getPage(), model.getRows());
	}

	@Override
	public int updateRateInterest(long id, int status) {
		return rateInterestDao.updateRateInterest(id, status);
	}

	@Override
	public int countRateInterest(long userId) {
		return rateInterestDao.countRateInterest(userId);
	}

	@Override
	public List<RateInterest> getRateInterestList(long userId) {
		return rateInterestDao.getRateInterestByUId(userId);
	}

	@Override
	public PageDataList<RateInterest> getRateInterestList(long userId, int status, int currentPage, int pageSize) {
		PageProxyBuilder<RateInterestMapper> proxy = PageProxyBuilder.newBuilder(RateInterestMapper.class);
		proxy.setPageParameter(currentPage, pageSize);
		List<RateInterest> list = proxy.createProxy().findByUserId(userId, status);
		PageParameter pageParameter = proxy.getPageParameter();
		PageDataList<RateInterest> pageDateList = new PageDataList<RateInterest>(pageParameter.convert2Page(), list);
		return pageDateList;
	}

	@Override
	public List<RateRule> getUseRateRuleList() {
		QueryParam param = QueryParam.getInstance();
		param.addParam("isUse", RateRuleModel.RATERULE_STATUS_ENABLE);
		List<RateRule> list = rateRuleDao.findByCriteria(param);
		return list;
	}

    @Override
    public void rateInterestExpiredDispose() {
        rateInterestDao.rateInterestExpiredDispose();
    }

}
